<?php

namespace App\Models;

class Messages extends Base
{
    protected $table = 'messages';

    const TYPE_1 = 1;
    const TYPE_2 = 2;
    const TYPE_3 = 3;
    const TYPE_4 = 4;
    const TYPE_5 = 5;

    const TYPE_TEXT = [
        self::TYPE_1 => '<span style="width: 65px" class="el-tag">全部</span>',
        self::TYPE_2 => '<span style="width: 65px" class="el-tag">供应商</span>',
        self::TYPE_3 => '<span style="width: 65px" class="el-tag">人力HR</span>',
        self::TYPE_4 => '<span style="width: 65px" class="el-tag">其他</span>',
        self::TYPE_5 => '<span style="width: 65px" class="el-tag">中标记录</span>',
    ];

    protected $fillable = [
        'id','title','content','type','status','created_at','updated_at','user_ids'
    ];

    protected $casts = ['user_ids' => 'array'];

    /**
     * 获取列表
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @param int $limit 分页数量
     * @return mixed
     */
    protected function getList($params = [], $fields = ['*'], $limit = 10){
        $lists = $this->select($fields)
            ->where(function ($query) use ($params) {
                if(isset($params['kwd']) && $params['kwd']) {
                    $query ->where('title','like','%' . $params['kwd'] . '%');
                }
            })->where('status','<>',-1)
            ->orderBy('created_at', 'desc')
            ->paginate($limit);
        $this->stateToText($lists,[
            'type' => self::TYPE_TEXT,
        ]);
        return $lists;
    }

    /**
     * 发送系统消息
     * @param string $title 标题
     * @param string $content 内容
     * @param int $user_id 查看用户id
     * @return mixed
     */
    protected function sendMessages($title = '', $content = '', $user_id = '', $type = 1){
        $arr = array(
            'title' => $title,
            'content' => $content,
            'user_id' => $user_id,
            'type' => $type,
        );
        Messages::create($arr);
    }

}
